package com.wr.recruit.service.apis;

import com.google.common.collect.Lists;
import com.wr.recruit.service.apis.dto.CompanyDto;
import com.wr.recruit.service.dao.dbo.CompanyDbo;
import com.wr.recruit.service.service.CompanyService;
import com.wr.recruit.service.utils.APIResponse;
import com.wr.recruit.service.utils.APIResponseSet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;
import java.util.UUID;

import static com.wr.recruit.service.utils.Constants.*;

@RestController
public class CompanyApis {

    private CompanyService companyService;

    @Autowired
    public CompanyApis(CompanyService companyService) {
        this.companyService = companyService;
    }

    @PostMapping(value = "/company/create", produces = "application/json")
    @ResponseBody
    public APIResponse rsCompanyCreate(@RequestBody CompanyDto company){
        if (Objects.isNull(company.getCompanyName()))
            return APIResponse.getFailedResponse(String.format(PARAM_NOT_FOUND, COMPANY_NAME));
        if (Objects.isNull(company.getUnifiedSocialCreditCode()))
            return APIResponse.getFailedResponse(String.format(PARAM_NOT_FOUND, UNIFIED_SOCIAL_CREDIT_CODE));
        CompanyDbo companyDbo = new CompanyDbo(UUID.randomUUID().toString(), company.getCompanyName(), company.getCompanySize(),
                company.getCompanyAddress(), company.getRegisteredTime(), company.getRegisteredCapital(),
                company.getCompanyWeb(), company.getUnifiedSocialCreditCode(), false, "");
        CompanyDbo companyInfo = companyService.createCompanyInfo(companyDbo);
        if (Objects.isNull(companyInfo))
            return new APIResponseSet<>(false,"创建公司失败");
        return new APIResponseSet<>(true,"创建公司成功", Lists.newArrayList(companyInfo));
    }

    @PostMapping(value = "/company/update", produces = "application/json")
    @ResponseBody
    public APIResponse rsCompanyUpdate(@RequestBody CompanyDto company){
        CompanyDbo companyDbo = new CompanyDbo(company.getId(),company.getCompanyCode(), company.getCompanyName(), company.getCompanySize(),
                company.getCompanyAddress(), company.getRegisteredTime(), company.getRegisteredCapital(),
                company.getCompanyWeb(), company.getUnifiedSocialCreditCode(), TRUE.equals(company.getStatus()),
                company.getAttachmentCodes());
        CompanyDbo companyInfo = companyService.updateCompanyInfo(companyDbo);
        if (Objects.isNull(companyInfo))
            return new APIResponseSet<>(false,"更新公司失败");
        return new APIResponseSet<>(true,"更新公司成功", Lists.newArrayList(companyInfo));
    }

    @PostMapping(value = "/company/delete", produces = "application/json")
    @ResponseBody
    public APIResponse rsCompanyDelete(@RequestBody CompanyDto company){

        companyService.RevokeCompanyInfo(company.getCompanyCode());
        return new APIResponseSet<>(true,"删除公司成功");
    }

    @GetMapping(value = "/company/{companyCode}", produces = "application/json")
    @ResponseBody
    public APIResponse rsCompanyGet(@PathVariable("companyCode") String companyCode){
        CompanyDbo companyInfo = companyService.getCompanyInfoByCompanyCode(companyCode);
        return new APIResponseSet<>(true,"查询公司成功",Lists.newArrayList(companyInfo));
    }

    @GetMapping(value = "/company", produces = "application/json")
    @ResponseBody
    public APIResponse rsCompanyListFuzzyGet(@RequestParam(value = "companyName", required = false) String companyName,
                                        @RequestParam(value = "pageSize") Integer pageSize,
                                        @RequestParam(value = "pageNum") Integer pageNum){
        Page<CompanyDbo> companyInfo;
        if (Objects.isNull(companyName) || "".equals(companyName)){
            companyInfo = companyService.getAllCompanyList(pageSize,pageNum);
        } else {
            companyInfo = companyService.getCompanyListByCompanyName(companyName,pageSize,pageNum);
        }
        return new APIResponseSet<>(true,"查询公司成功",Lists.newArrayList(companyInfo));
    }

}
